package com.example.control;


import com.example.entry.Student;
import com.example.service.MessageService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

@Controller
public class MainController {

    @Resource
    MessageService MessageService;


//    这里是显示所有信息
    @RequestMapping(value = "allMessage")
    public String getBooksList(@RequestParam(value = "name",required = false)String name,Model model) {
        model.addAttribute("books",name==null?MessageService.list("%"):MessageService.list("%"+name+"%"));
        return "allMessage";
    }

    //    这里是添加过后的逻辑
    @RequestMapping(value = "addED",method = RequestMethod.POST)
    public String addBook(@RequestParam("name")String name, @RequestParam("no")String no, @RequestParam("sex")String sex, @RequestParam("clas")String clas, @RequestParam("tel")String tel, @RequestParam("adde")String adde, @RequestParam("coll")String coll){
        MessageService.add(name,no,sex,clas,tel,adde,coll);
        return "redirect:/allMessage";
    }


//    这里是添加之前的逻辑
    @RequestMapping("/adding")
    public String index4() {
        return "addMessage";
    }


    //    这里是删除逻辑，删除完了直接重定向
    @RequestMapping(value = "delBook",method = RequestMethod.GET)
    public String addBook(@RequestParam("id") int id){
        MessageService.Del(id);
        return "redirect:allMessage";
    }

//    账户登录逻辑
    @RequestMapping("/b")
    public String test1(@RequestParam("username") String username, @RequestParam("password") String password, Model model){
        return MessageService.findUser(username,password)==null? "/index": "redirect:/allMessage";
    }


    //    修改前
    @RequestMapping("/update")
    public String update(@RequestParam("id") int id,Model model){
        model.addAttribute("book",MessageService.findByID(id));
        return "updateMessage";
    }

//    修改后重定向
    @RequestMapping("/update2")
    public String updateBook2(@ModelAttribute(value = "demo") Student demo, Model model){
        MessageService.update(demo.getName(),demo.getNo(),demo.getSex(),demo.getClas(),demo.getTel(),demo.getAdde(),demo.getColl(),demo.getId());
        return "redirect:allMessage";
    }



}
